<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-details-buttons xs:flex-col">
  <div class="flex flex-row pl-2 xs:flex-col">
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'open')"
      [class.!hidden]="!(!isEdit && !isDetailsPage)">
      {{'common.open-details-page' | translate }}
    </button>
    <button mat-raised-button color="primary"
      [disabled]="(isLoading$ | async)"
      (click)="onEntityAction($event, 'delete')"
      [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:trash-can-outline"></mat-icon>
      <span translate>shared-subscription.delete</span>
    </button>
  </div>
  <div class="flex flex-row pl-2 xs:flex-col">
    <tb-copy-content-button [cbContent]="entity?.id" [isEdit]="isEdit"></tb-copy-content-button>
  </div>
</div>
<div class="mat-padding flex flex-col">
  <fieldset class="fields-group">
    <legend class="group-title">
      <mat-icon class="material-icons">info_outline</mat-icon>
    </legend>
    <div class="tb-hint" [innerHTML]="'shared-subscription.hint-application-shared-subscription' | translate"></div>
  </fieldset>
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit">
      <mat-form-field class="mat-block mt-4">
        <mat-label translate>shared-subscription.name</mat-label>
        <input matInput formControlName="name" required>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'shared-subscription.name-required' | translate }}"
                  [class.!hidden]="!(entityForm.get('name').hasError('required') && entityForm.get('name').touched)"
                  class="tb-error">
          warning
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="mat-block">
        <mat-label translate>shared-subscription.topic-filter</mat-label>
        <input matInput formControlName="topicFilter" required [matAutocomplete]="auto">
        <mat-autocomplete #auto="matAutocomplete">
          @for (option of filteredTopics | async; track option) {
            <mat-option [value]="option">{{option}}</mat-option>
          }
        </mat-autocomplete>
        <tb-copy-button #copyBtn
          (click)="onClickTbCopyButton(entityForm.get('topicFilter')?.value)"
          [class.!hidden]="!entityForm.get('topicFilter')?.value?.length" matSuffix [disabled]="isLoading$ | async"
          [copyText]="entityForm.get('topicFilter').value">
        </tb-copy-button>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'shared-subscription.topic-filter-required' | translate }}"
                  [class.!hidden]="!(entityForm.get('topicFilter').hasError('required') && entityForm.get('topicFilter').touched)"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix matTooltip="{{ 'shared-subscription.hint-topic-filter' | translate }}">
          help
        </mat-icon>
      </mat-form-field>
      <mat-form-field class="mat-block">
        <mat-label translate>shared-subscription.partitions</mat-label>
        <input matInput formControlName="partitions" required type="number" min="1" step="1">
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'shared-subscription.partitions-required' | translate }}"
                  [class.!hidden]="!(entityForm.get('partitions').hasError('required') && entityForm.get('partitions').touched)"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix
                  matTooltipClass="tb-error-tooltip"
                  matTooltip="{{ 'shared-subscription.partitions-min' | translate }}"
                  [class.!hidden]="!(entityForm.get('partitions').hasError('min') && entityForm.get('partitions').touched)"
                  class="tb-error">
          warning
        </mat-icon>
        <mat-icon matSuffix matTooltip="{{ 'shared-subscription.hint-partitions' | translate }}">
          help
        </mat-icon>
      </mat-form-field>
      @if (!entity?.name) {
        <div class="tb-hint" [innerHTML]="'shared-subscription.hint-shared-subscription-notes' | translate"></div>
      }
    </fieldset>
  </form>
</div>
